package cn.scxy.bookcity.controller;

import cn.scxy.bookcity.pojo.dto.UserAddDTO;
import cn.scxy.bookcity.pojo.dto.UserLoginDTO;
import cn.scxy.bookcity.pojo.dto.UserUpdateDTO;
import cn.scxy.bookcity.pojo.dto.UserUpdatePasswordDTO;
import cn.scxy.bookcity.pojo.vo.UserStandardVO;
import cn.scxy.bookcity.service.impl.UserServiceImpl;
import cn.scxy.bookcity.web.JsonResult;
import cn.scxy.bookcity.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@Validated
@RequestMapping("/user")
@Api(tags = "用户模块")
@RestController
public class UserController {
    @Autowired
    UserServiceImpl userServiceImpl;

    @ApiOperation("用户添加信息")
    @ApiOperationSupport(order = 100)
    @PostMapping("/addNew")
    public JsonResult<Void> addNew(UserAddDTO userAddDTO){
        userServiceImpl.insert(userAddDTO);
        return JsonResult.ok();
    }

    @ApiOperation("用户添加个人信息")
    @ApiOperationSupport(order = 100)
    @PostMapping("/submit")
    public JsonResult<Void> submit(UserUpdateDTO userUpdateDTO,HttpSession session){
        log.debug("用户提交的信息,{}",userUpdateDTO);
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        userUpdateDTO.setId(id);
        userServiceImpl.updateByuId(userUpdateDTO);
        return JsonResult.ok();
    }

    @ApiOperation("修改密码")
    @ApiOperationSupport(order = 300)
    @PostMapping("/updatePassword")
    public JsonResult<Void> updatePassword(UserUpdatePasswordDTO userUpdatePasswordDTO,
                                            HttpSession session){
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        userUpdatePasswordDTO.setId(id);
        log.debug("用户id:{}",id);
        userServiceImpl.update(userUpdatePasswordDTO);
        return JsonResult.ok();
    }

    @ApiOperation("查询安全等级")
    @ApiOperationSupport(order = 400)
    @PostMapping("/security")
    public JsonResult<String> security(HttpSession session){
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        String message = userServiceImpl.security(id);
        return JsonResult.ok(message);
    }

    @ApiOperation("查询手机号是否设置")
    @ApiOperationSupport(order = 401)
    @PostMapping("/havePhone")
    public JsonResult<String> havePhone(HttpSession session){
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        log.debug("获取到用户id为{}",id);
        String message = userServiceImpl.havePhone(id);
        return JsonResult.ok(message);
    }

    @ApiOperation("查询邮箱是否设置")
    @ApiOperationSupport(order = 402)
    @PostMapping("/haveEmail")
    public JsonResult<String> haveEmail(HttpSession session){
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        log.debug("获取到用户id为{}",id);
        String message = userServiceImpl.haveEmail(id);
        return JsonResult.ok(message);
    }

    @ApiOperation("查询手机号")
    @ApiOperationSupport(order = 403)
    @PostMapping("/phone")
    public JsonResult<String> phone(HttpSession session){
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        log.debug("获取到用户id为{}",id);
        String message = userServiceImpl.selectPhone(id);
        log.debug("查询到的手机号为:{}",message);
        return JsonResult.ok(message);
    }

    @ApiOperation("查询用户信息")
    @ApiOperationSupport(order = 404)
    @PostMapping("/standard")
    public JsonResult<UserStandardVO> info(HttpSession session){
        UserLoginDTO userLoginDTO= (UserLoginDTO) session.getAttribute("user");
        Integer id = userLoginDTO.getId();
        log.debug("获取到用户id为{}",id);
        UserStandardVO userStandardVO = userServiceImpl.getStandardByuId(id);
        return JsonResult.ok(userStandardVO);
    }
}
